package transactions.dq.masterdetail;

import dinamica.Db;
import dinamica.GenericTableManager;
import dinamica.Recordset;
import dinamica.StringUtil;

public class MasterDetail extends GenericTableManager
{
	
	public int service(Recordset inputParams) throws Throwable
	{
		int rc = super.service(inputParams);
		//获取sql文件
		String delete = getSQL(getResource("delete.sql"),inputParams);
		String update_master = getSQL(getResource("update_master.sql"),inputParams);
		String insert = getResource("insert_master_detail.sql");
		//获取页面数据集
		String user_name[] = getRequest().getParameterValues("user_name");
		String relation_to_master[] = getRequest().getParameterValues("relation_to_master");
		String user_sex[] = getRequest().getParameterValues("user_sex");
		String user_birth[] = getRequest().getParameterValues("user_birth");
		String hukoubu_organ[] = getRequest().getParameterValues("hukoubu_organ");
		String hukoubu_createdate[] = getRequest().getParameterValues("hukoubu_createdate");
		String hukoubu_id[] = getRequest().getParameterValues("hukoubu_id");
		String jiehunzheng_organ[] = getRequest().getParameterValues("jiehunzheng_organ");
		String jiehunzhen_id[] = getRequest().getParameterValues("jiehunzhen_id");
		String jiehunzheng_createdate[] = getRequest().getParameterValues("jiehunzheng_createdate");
		String remark[] = getRequest().getParameterValues("remark");
		String user_idcard[] = getRequest().getParameterValues("user_idcard");
		
		Db db = getDb();
		//先删除原数据，再重新插入新数据
		db.addBatchCommand(delete);
		
		//必填项不能为空
		if(user_name==null || relation_to_master==null || user_sex==null){
			throw new Throwable("数据填写不完整，请确认后再提交。");
		}
		int length = user_name.length;
		
		//添加批处理插入语句
		for(int i=0;i<length;i++)
		{
			if(user_name[i] == null || user_name[i].trim().length() == 0){
				throw new Throwable("姓名不能为空，请确认后再提交。");
			}
			if(relation_to_master[i] == null || relation_to_master[i].trim().length() == 0){
				throw new Throwable("与户主关系不能为空，请确认后再提交。");
			}
			if(user_sex[i] == null || user_sex[i].trim().length() == 0){
				throw new Throwable("性别不能为空，请确认后再提交。");
			}
			if(remark[i] != null && remark[i].trim().length() > 60){
				throw new Throwable("备注输入太长，不能超过60个字符，请确认后再提交。");
			}
			
			String insert_sql = "";
			insert_sql = StringUtil.replace(insert, "${user_name}", user_name[i]);
			insert_sql = StringUtil.replace(insert_sql, "${relation_to_master}", relation_to_master[i]);
			insert_sql = StringUtil.replace(insert_sql, "${user_sex}", user_sex[i]);
			insert_sql = StringUtil.replace(insert_sql, "${user_birth}", (user_birth[i]==null || user_birth[i].trim().length()==0)?"NULL":("{d '"+user_birth[i]+"'}"));
			insert_sql = StringUtil.replace(insert_sql, "${hukoubu_organ}", hukoubu_organ[i]);
			insert_sql = StringUtil.replace(insert_sql, "${hukoubu_createdate}", (hukoubu_createdate[i]==null || hukoubu_createdate[i].trim().length()==0)?"NULL":("{d '"+hukoubu_createdate[i]+"'}"));
			insert_sql = StringUtil.replace(insert_sql, "${hukoubu_id}", hukoubu_id[i]);
			insert_sql = StringUtil.replace(insert_sql, "${jiehunzheng_organ}", jiehunzheng_organ[i]);
			insert_sql = StringUtil.replace(insert_sql, "${jiehunzhen_id}", jiehunzhen_id[i]);
			insert_sql = StringUtil.replace(insert_sql, "${jiehunzheng_createdate}", (jiehunzheng_createdate[i]==null || jiehunzheng_createdate[i].trim().length()==0)?"NULL":("{d '"+jiehunzheng_createdate[i]+"'}"));
			insert_sql = StringUtil.replace(insert_sql, "${remark}", remark[i]);
			insert_sql = StringUtil.replace(insert_sql, "${user_idcard}", user_idcard[i]);
			
			if("0".equals(relation_to_master[i])){//0是户主
				insert_sql = StringUtil.replace(insert_sql, "${is_master}", "1");
			}else{
				insert_sql = StringUtil.replace(insert_sql, "${is_master}", "0");
			}
			insert_sql = getSQL(insert_sql,inputParams);
			db.addBatchCommand(insert_sql);
		}
		
		db.exec();
		db.exec(update_master);
		return rc;
	}

}
